﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query for the thrift call get_slice. Get a slice of super counter columns from a super column family.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class ThriftSuperSliceCounterQuery<K, SN, N> : AbstractSliceQuery<K, SN, long, CounterSuperSlice<SN, N>>, SuperSliceCounterQuery<K, SN, N>
    {
        public ThriftSuperSliceCounterQuery(Keyspace keyspace, Serializer<K> keySerializer,
            Serializer<SN> sNameSerializer, Serializer<N> nameSerializer) :
            base(keyspace, keySerializer, sNameSerializer, LongSerializer.Instance)
        {
            if (sNameSerializer == null)
            {
                throw new NullReferenceException("SuperNameSerializer cannot be null");
            }
            NameSerializer = nameSerializer;
        }
        public K Key
        {
            get;
            set;
        }
        public Serializer<N> NameSerializer
        {
            get;
            set;
        }
        public SuperSliceCounterQuery<K, SN, N> SetKey(K key)
        {
            Key = key;
            return this;
        }
        public override string ToString()
        {
            return "SuperSliceQuery(" + Key + "," + ToStringInternal() + ")";
        }
        public new SuperSliceCounterQuery<K, SN, N> SetColumnNames(List<SN> columnNames)
        {
            return (SuperSliceCounterQuery<K, SN, N>) base.SetColumnNames(columnNames);
        }
        public new SuperSliceCounterQuery<K, SN, N> SetRange(SN start, SN finish, bool reversed, int count)
        {
            return (SuperSliceCounterQuery<K, SN, N>) base.SetRange(start, finish, reversed, count);
        }
        public new SuperSliceCounterQuery<K, SN, N> SetColumnFamily(string cf)
        {
            return (SuperSliceCounterQuery<K, SN, N>) base.SetColumnFamily(cf);
        }
        public override QueryResult<CounterSuperSlice<SN, N>> Execute()
        {
            Func<KeyspaceService, CounterSuperSlice<SN,N>> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                List<CounterSuperColumn> thriftRet = ks.GetCounterSuperSlice(KeySerializer.ToBytes(Key),columnParent,GetPredicate());
                return new CounterSuperSliceImpl<SN, N>(thriftRet, ColumnNameSerializer, NameSerializer);
            };
            return new QueryResultImpl<CounterSuperSlice<SN,N>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<CounterSuperSlice<SN,N>>(x)), this);
        }
    }
}
